package school.xauat.inscription_sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import school.xauat.inscription_sys.mapper.UserMapper;
import school.xauat.inscription_sys.pojo.User;
import school.xauat.inscription_sys.service.UserService;

import static school.xauat.inscription_sys.errorcode.ErrorCode.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zsy
 * @since 2022-04-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public int checkUser(String username, String password) {
        User db_user;
        try {
            db_user = userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
        } catch (Exception e) {
            log.error(String.format("db err:%s", e.getMessage()));
            return ERROR_DB_UNKNOWN;
        }
        if (db_user == null || !password.equals(db_user.getPassword())) {
            return ERROR_USERNAME_PASSWORD_FAILED;
        }
        return SUCCESS;
    }
}
